package com.eightbears.beboy.adapter;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.eightbears.beboy.R;
import com.eightbears.beboy.base.util.CommonUtils;
import com.eightbears.beboy.base.view.image.view.ImageItem;
import com.eightbears.beboy.base.view.layout.WrappableGridLayoutManager;
import com.eightbears.beboy.entity.FriendCirleEntity;
import com.eightbears.beboy.entity.HomeListEntity;

import java.util.List;

/**
 * Created by Bao on 2016/12/7.
 */

public class MyDynamicAdapter extends BaseQuickAdapter<FriendCirleEntity.DataBean.ListBean, BaseViewHolder> {

    private OnClickImageItemListener mClickImageListener;
    public String time = "0";
    private ImageView iv_img;
    private TextView tv_time;
    private RecyclerView mRv_list;
    private TextView mTv_num;

    // 图片查看单击事件
    public interface OnClickImageItemListener {
        void onImageClick(BaseQuickAdapter baseViewHolder, View view, int i);
    }

    public void setOnImageClickListener(OnClickImageItemListener clickListener) {
        this.mClickImageListener = clickListener;
    }

    public MyDynamicAdapter() {
        super(R.layout.item_my_dynamic, null);
    }

    @Override
    protected void convert(BaseViewHolder baseViewHolder, FriendCirleEntity.DataBean.ListBean listBean) {

        showTexts(baseViewHolder, listBean);
        showImages(baseViewHolder, listBean);
        tv_time = baseViewHolder.getView(R.id.tv_time);
        mTv_num = baseViewHolder.getView(R.id.tv_num);
        if (listBean.isHeadTitle())
            showLeft();
        else
            hindLeft();
//        if (listBean.getUser() != null && listBean.getUser().getCover() != null) {
//            CommonUtils.loadImageView(mContext,
//                    homeMessageEntity.getRepost().getImages().get(0), R.mipmap.default_icon, iv_img);
//        } else if (homeMessageEntity.getImages() != null) {
//            CommonUtils.loadImageView(mContext,
//                    homeMessageEntity.getImages().get(0), R.mipmap.default_icon, iv_img);
//        }
        tv_time.setText(CommonUtils.TimeStamp2Date(listBean.getCreated_at(), "MM") + "月");
        mTv_num.setText(CommonUtils.TimeStamp2Date(listBean.getCreated_at(), "dd"));
    }

    //显示文字
    private void showTexts(BaseViewHolder baseViewHolder, FriendCirleEntity.DataBean.ListBean listBean) {
        if (listBean.getContent() != null && listBean.getContent().length() > 0) {
            TextView tv_content = baseViewHolder.getView(R.id.tv_content);
            tv_content.setVisibility(View.VISIBLE);
            tv_content.setText(listBean.getContent());
        }
    }

    // 显示图片
    private void showImages(BaseViewHolder baseViewHolder, FriendCirleEntity.DataBean.ListBean listBean) {
        if (listBean.getImages() != null && listBean.getImages().size() > 0) {
            mRv_list = baseViewHolder.getView(R.id.rv_list);
            mRv_list.setVisibility(View.VISIBLE);
            initImagesAdapter(listBean.getImages());
        }
    }

    /**
     * 初始化图片适配
     */
    private void initImagesAdapter(List<String> images) {
        MyDynamicAdapter.ImagesAdapter mImagesAdapter = new MyDynamicAdapter.ImagesAdapter();
        WrappableGridLayoutManager mWrapHeightGridLayoutManager = new WrappableGridLayoutManager(mContext, 3);
        mRv_list.setLayoutManager(mWrapHeightGridLayoutManager);
        mImagesAdapter.setNewData(ImageItem.replaceImage(images));
        mRv_list.setAdapter(mImagesAdapter);
//        Glide.with(mContext).load(images.get(0)).crossFade().error(R.mipmap.default_icon).into(iv_shop_icon);
        mImagesAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if (null != mClickImageListener)
                    mClickImageListener.onImageClick(adapter, view, position);
            }
        });
    }

    //图片适配
    private class ImagesAdapter extends BaseQuickAdapter<ImageItem, BaseViewHolder> {
        public ImagesAdapter() {
            super(R.layout.item_image_dynamic, null);
        }

        @Override
        protected void convert(BaseViewHolder baseViewHolder, ImageItem image) {
            Glide.with(mContext).load(image.path).into((ImageView) baseViewHolder.getView(R.id.iv_item));
            baseViewHolder.addOnClickListener(R.id.iv_item);
        }
    }


    private void showLeft() {
        tv_time.setVisibility(View.VISIBLE);
        mTv_num.setVisibility(View.VISIBLE);
    }

    private void hindLeft() {
        tv_time.setVisibility(View.GONE);
        mTv_num.setVisibility(View.GONE);
    }

}
